package a.netty.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

@ChannelHandler.Sharable
public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    protected void messageReceived(ChannelHandlerContext context, ByteBuf byteBuf) {
        System.out.println("client - ClientHandler.messageReceived");
        System.out.println("接收到的消息：" + byteBuf.toString(CharsetUtil.UTF_8));
        context.fireChannelRead(byteBuf);
    }

    /**
     * 1.服务端失联
     *
     * @param ctx
     * @param cause
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        System.out.println("HANDLER_ERROR::" + cause.getMessage());
        ctx.close();
    }
}
